//
//  HcCustomKeyboard.m
//  Custom
//
//  Created by 黄诚 on 14/12/18.
//  Copyright (c) 2014年 huangcheng. All rights reserved.
//

#import "HcCustomKeyboard.h"
#import <UIKit/UIKit.h>

//屏幕宽度
#define WIDTH_SCREEN [UIScreen mainScreen].bounds.size.width
//屏幕高度
#define HEIGHT_SCREEN [UIScreen mainScreen].bounds.size.height

#define SHOWWORD  40
@implementation HcCustomKeyboard
@synthesize mDelegate;

static HcCustomKeyboard *customKeyboard = nil;

+(HcCustomKeyboard *)customKeyboard
{
    @synchronized(self)
    {
        if (customKeyboard == nil)
        {
            customKeyboard = [[HcCustomKeyboard alloc] init];
        }
        return customKeyboard;
    }
}
+(id)allocWithZone:(struct _NSZone *)zone //确保使用者alloc时 返回的对象也是实例本身
{
    @synchronized(self)
    {
        if (customKeyboard == nil)
        {
            customKeyboard = [super allocWithZone:zone];
        }
        return customKeyboard;
    }
}
+(id)copyWithZone:(struct _NSZone *)zone //确保使用者copy时 返回的对象也是实例本身
{
    return customKeyboard;
}

-(void)textViewShowView:(UIViewController *)viewController customKeyboardDelegate:(id)delegate
{
    self.mViewController =viewController;
    self.mDelegate =delegate;
    self.isTop = NO;//初始化的时候设为NO
    
    self.mBackView =[[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT_SCREEN, WIDTH_SCREEN, 40)];

    self.mBackView.backgroundColor =[UIColor whiteColor];
    [self.mViewController.view addSubview:self.mBackView];
    
    self.mSecondaryBackView =[[UIView alloc]initWithFrame:CGRectMake(10, 5, WIDTH_SCREEN-20, 30)];
    self.mSecondaryBackView.backgroundColor =COLOR(239, 239, 239);
    _mSecondaryBackView.layer.masksToBounds=YES;
    _mSecondaryBackView.layer.cornerRadius=5;
    _mSecondaryBackView.layer.borderColor=[COLOR(220, 220, 220) CGColor];
    _mSecondaryBackView.layer.borderWidth=0.5;
    [self.mBackView addSubview:self.mSecondaryBackView];
    
    self.mTextView =[[UITextView alloc]initWithFrame:CGRectMake(0, 0,  WIDTH_SCREEN-20-SHOWWORD, 30)];
    self.mTextView.backgroundColor =[UIColor clearColor];
  
    self.mTextView.delegate = self;
    self.mTextView.text =nil;
    _mTextView.returnKeyType = UIReturnKeySend;
     _mTextView.enablesReturnKeyAutomatically = YES;//有文字就有效，否则无效
    
    self.mTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;;
    [self.mSecondaryBackView addSubview:self.mTextView];
    
    _placeHoldLable=[[UILabel alloc] initWithFrame:CGRectMake(5, 0, _mTextView.frame.size.width-5,_mTextView.frame.size.height)];
    _placeHoldLable.textColor=[UIColor lightTextColor];
    _placeHoldLable.font=[UIFont systemFontOfSize:11];
    _placeHoldLable.text=@"我来说两句...";
    _placeHoldLable.enabled = NO;
    [_mTextView addSubview:_placeHoldLable];
    
    _wordLenghtShow=[[UILabel alloc] initWithFrame:CGRectMake(_mSecondaryBackView.frame.size.width-SHOWWORD, 0,SHOWWORD,_mSecondaryBackView.frame.size.height)];
    _wordLenghtShow.textColor=COLOR(100, 100, 100);
    _wordLenghtShow.font=[UIFont systemFontOfSize:9];
    _wordLenghtShow.text=@"0/140";
    _wordLenghtShow.numberOfLines=2;
    _wordLenghtShow.enabled = YES;
    [_mSecondaryBackView addSubview:_wordLenghtShow];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
   
    
}
-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        _placeHoldLable.text=@"我来说两句...";
    }else{
        _placeHoldLable.text=@"";
    }
    CGSize contentSize = self.mTextView.contentSize;
    
    if (contentSize.height > 140){
        return;
    }
    if(textView.text.length>140)
    {
        _wordLenghtShow.textColor=COLOR(163, 53, 35);
    }else
    {
        _wordLenghtShow.textColor=COLOR(100, 100, 100);
    }
    _wordLenghtShow.text=[NSString stringWithFormat:@"%@/140",@(textView.text.length)];
   
    CGRect selfFrame = self.mBackView.frame;
    CGFloat selfHeight = self.mTextView.superview.frame.origin.y * 2 + contentSize.height + 2;
    CGFloat selfOriginY = selfFrame.origin.y - (selfHeight - selfFrame.size.height);
    selfFrame.origin.y = selfOriginY;
    selfFrame.size.height = selfHeight;
    self.mBackView.frame = selfFrame;
    self.mSecondaryBackView.frame =CGRectMake(10, 5, WIDTH_SCREEN-20, selfHeight-10);
     self.mTextView.frame =CGRectMake(0, 0, WIDTH_SCREEN-20-SHOWWORD, selfHeight-10);
    _wordLenghtShow.frame=CGRectMake(_mSecondaryBackView.frame.size.width-SHOWWORD,0,SHOWWORD,_mSecondaryBackView.frame.size.height);
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        if ([mDelegate respondsToSelector:@selector(talkBtnClick:typeTag:withReplyUserId:rootTalkId:)]) {
            [mDelegate talkBtnClick:self.mTextView typeTag:_typeTag withReplyUserId:_replyUserId rootTalkId:_rootTalkId];
        }
        _mTextView.text=nil;
        [self.mTextView resignFirstResponder];
        self.mBackView.frame=CGRectMake(0, HEIGHT_SCREEN, WIDTH_SCREEN, 40);
        self.mSecondaryBackView.frame =CGRectMake(10, 5, WIDTH_SCREEN-20,30);
        _placeHoldLable.text=@"我来说两句...";
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{

    return YES;
}


-(void)hideView //点击屏幕其他地方 键盘消失
{
    [self.mTextView resignFirstResponder];
}

- (void)keyboardWillShow:(NSNotification*)notification //键盘出现
{
    self.isTop =YES;
    CGRect _keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat animDuration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    if (!self.mHiddeView)
    {
        self.mHiddeView =[[UIView alloc]initWithFrame:CGRectMake(0, 0,WIDTH_SCREEN,HEIGHT_SCREEN)];
        self.mHiddeView.backgroundColor =[UIColor blackColor];
        self.mHiddeView.alpha =0.0f;
        [self.mViewController.view addSubview:self.mHiddeView];
        
        UIButton *hideBtn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
        hideBtn.backgroundColor =[UIColor clearColor];
        hideBtn.frame = self.mHiddeView.frame;
        [hideBtn addTarget:self action:@selector(hideView) forControlEvents:UIControlEventTouchUpInside];
        [self.mHiddeView addSubview:hideBtn];
    }
    
    [UIView animateWithDuration:animDuration animations:^{
        self.mHiddeView.alpha =0.4f;
        self.mBackView.frame =CGRectMake(0, HEIGHT_SCREEN-_keyboardRect.size.height-_mBackView.frame.size.height, WIDTH_SCREEN,  _mBackView.frame.size.height);
        self.mSecondaryBackView.frame =CGRectMake(10, 5, WIDTH_SCREEN-20,_mSecondaryBackView.frame.size.height);
        self.mTextView.frame =CGRectMake(0, 0, WIDTH_SCREEN-20-SHOWWORD,_mSecondaryBackView.frame.size.height);
        [self.mViewController.view bringSubviewToFront:self.mBackView];
    } completion:^(BOOL finished) {
        
    }];
}
- (void)keyboardWillHide:(NSNotification*)notification //键盘下落
{
     CGFloat animDuration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    self.isTop =NO;
        [UIView animateWithDuration:animDuration animations:^{
        self.mBackView.frame=CGRectMake(0, HEIGHT_SCREEN, WIDTH_SCREEN, _mBackView.frame.size.height);
        self.mHiddeView.alpha =0.0f;
        self.mSecondaryBackView.frame =CGRectMake(10, 5, WIDTH_SCREEN-20,_mSecondaryBackView.frame.size.height);
        self.mTextView.frame =CGRectMake(0, 0, WIDTH_SCREEN-20-SHOWWORD,_mSecondaryBackView.frame.size.height);
    } completion:^(BOOL finished) {
        [self.mHiddeView removeFromSuperview];
        self.mHiddeView =nil;
    }];
}

-(void)talkAndReplyTextWithTypeTag:(NSInteger)typeTag withReplyUserId:(NSInteger)replyUserId rootTalkId:(NSInteger)rootTalkId
{
    _typeTag=typeTag;
    _replyUserId=replyUserId;
    _rootTalkId=rootTalkId;
    
    [_mTextView becomeFirstResponder];
}

-(void)dealloc //移除通知
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


@end

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com 
